package com.platform.hjt.sys.service;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.hjt.tools.dao.Page;
import com.hjt.tools.dao.PropertyFilter;
import com.hjt.tools.date.DateCreator;
import com.platform.hjt.base.controller.ReturnBody;
import com.platform.hjt.base.service.BaseService;
import com.platform.hjt.sys.dao.SysCompanyInfoDAO;
import com.platform.hjt.sys.dao.SysCompanyModelDAO;
import com.platform.hjt.sys.model.SysCompanyInfo;
import com.platform.hjt.sys.model.SysCompanyModel;
import com.platform.hjt.sys.model.SysCompanyModelId;

@Service
public class SysCompanyInfoService extends BaseService{

	@Resource
	private SysCompanyInfoDAO sysCompanyInfoDAO;
	@Resource
	private SysCompanyModelDAO sysCompanyModelDAO;
	
	public ReturnBody saveSysCompanyInfo(SysCompanyInfo sysCompanyInfo){
		
		if(StringUtils.isNotBlank(sysCompanyInfo.getSysCompanyInfoId())){
			//先删除该公司的所有权限
			sysCompanyModelDAO.batchExecute("delete SysCompanyModel t where t.sysCompanyInfo.sysCompanyInfoId=?", sysCompanyInfo.getSysCompanyInfoId());
			sysCompanyInfoDAO.updatePerproties(sysCompanyInfo);
			addCompanyModel(sysCompanyInfo);
			return  new ReturnBody(true, "更新成功");
		} else{
			sysCompanyInfo.setIsDel(false);
			sysCompanyInfo.setCreateTime(DateCreator.getCurrentTime());
			sysCompanyInfoDAO.saveJ(sysCompanyInfo);
			addCompanyModel(sysCompanyInfo);
			return  new ReturnBody(true, "添加成功");
		}
	}
	
	/**
	 * 添加公司权限
	 * @Description 
	 * @author huangjt
	 * @param sysCompanyInfo
	 */
	private void addCompanyModel(SysCompanyInfo sysCompanyInfo){
		if(StringUtils.isNotBlank(sysCompanyInfo.getSysModelIds())){
			String[] arr = sysCompanyInfo.getSysModelIds().split(",");
			for(String id : arr){
				sysCompanyModelDAO.save(new SysCompanyModel(new SysCompanyModelId(id, sysCompanyInfo.getSysCompanyInfoId())));
			}
		}
	}
	
	public ReturnBody delSysCompanyInfo(SysCompanyInfo sysCompanyInfo){
		sysCompanyInfo.setIsDel(true);
		sysCompanyInfoDAO.updatePerproties(sysCompanyInfo);
		return  new ReturnBody(true, "删除成功");
	}
	
	public Page<SysCompanyInfo> getSysCompanyInfoList(Page<SysCompanyInfo> page, List<PropertyFilter> propertyFilterList){
		return sysCompanyInfoDAO.findPage(page, propertyFilterList);
	}
	
	public List<SysCompanyInfo> getSysCompanyInfoList(List<PropertyFilter> filters){
		return sysCompanyInfoDAO.find(filters);
	}
	
	public SysCompanyInfo getSysCompanyInfo(List<PropertyFilter> filters){
		return sysCompanyInfoDAO.findUniqueBy(filters);
	}
	
	public SysCompanyInfo getSysCompanyInfo(String propertyName, Object value){
		return sysCompanyInfoDAO.findUniqueBy(propertyName, value);
	}
	
	public List<SysCompanyInfo> getAll(){
		return sysCompanyInfoDAO.findBy("isDel", false);
	}
	
}
